package com.pjb.springbootjjwt.store.entity;

import com.pjb.springbootjjwt.store.Defines;
import com.pjb.springbootjjwt.store.File;
import com.pjb.springbootjjwt.store.Sql;
import com.pjb.springbootjjwt.store.storage.DDL;
import com.pjb.springbootjjwt.store.utils.StringUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 行标题
 */
@Data
public class Head {

    private List<String> data = new ArrayList<>();
    private List<String> source = new ArrayList<>();

    /**
     * 生成对象
     *
     * @param lineStr "a, b, c"
     */
    public static Head toHead(String lineStr) {
        if (StringUtils.isEmpty(lineStr) || lineStr.trim().isEmpty()) return null;

        Head head = new Head();
        List<String> list = new ArrayList<>(Arrays.asList(lineStr.split(",")));

        head.setData(list);
        head.setSource(list);
        return head;
    }

    public static Head toHeadByDDL(String ddl) {
        if (StringUtils.isEmpty(ddl) || ddl.trim().isEmpty()) return null;

        Head head = new Head();
        List<String> list = new ArrayList<>();
        if (!ddl.contains("view")) {
            list = Defines.getFields(ddl);
        }
        head.setData(list);
        head.setSource(list);
        return head;
    }

    public static Head toHeadByAlias(String sql) throws Exception {
        if (StringUtils.isEmpty(sql) || sql.trim().isEmpty()
                || !sql.toLowerCase().contains("select") || !sql.toLowerCase().contains("from")) return null;

        Head head = new Head();
        List<String> list = new ArrayList<>();
        List<String> source = new ArrayList<>();

        String subSql = sql.substring(sql.indexOf("select"), sql.indexOf("from")).replaceAll("select", "").trim();
        String[] arr = subSql.split(",");
        for (String field : arr) {
            String[] kv = field.trim().split("\\s+");
            list.add(kv[kv.length - 1]);
            source.add(kv[0]);
        }

        head.setData(list);
        head.setSource(source);

        String subFrom = StringUtil.sub(sql, "from", "where");
        if (!subFrom.contains("@")) {
            String[] fromArr = subFrom.toLowerCase().split(",");
            if (sql.contains("join")) {
                fromArr = Sql.getInstance().getTableArr(subFrom);
            }
            List<String> fields = new ArrayList<>();
            for (String tableStr : fromArr) {
                String table = tableStr.trim().split("\\s+")[0];
                String ddl = File.getInstance().getDDL(table);
                fields.addAll(Defines.getFields(ddl));
            }
            for (String s : source) {
                if (s.contains(".")) {
                    s = s.split("\\.")[1];
                }
                if (!fields.contains(s)) throw new Exception("" + Response.error.set(Error.E90008, s));
            }
        } else {
            System.out.println("todo......");
        }
        return head;
    }

    public static void main(String[] args) throws Exception {
        Head head = toHead("a, b, c");
        assert head != null;
        int i = head.getIndex("c");
        System.out.println(i);
        new DDL().use("test");
        String ddl = File.getInstance().getDDL("vv");


        System.out.println(head);
        System.out.println(head.getIndex("c"));

        head = toHeadByAlias("select e, a x1, b x2, c from vv");
        assert head != null;
        System.out.println(head.getIndex("x2"));
        System.out.println(head.getIndex("x1"));
    }

    public int getIndex(String field) {
        for (String k : data) {
            if (k.trim().equals(field.trim())) return data.indexOf(k);
        }
        return -1;
    }
}
